package org.batatasframework.support.charts;

import java.text.DecimalFormat;

@Deprecated
public abstract class ResultDataCell {
	
	public final static ResultDataCell cellNumber(){
		return new NumberChartDataCell();
	}

	public final static ResultDataCell cellNumber(DecimalFormat format){
		return new NumberChartDataCell(format);
	}

	public final static ResultDataCell cellDecimal(){
		return new NumberChartDataCell();
	}

	public final static ResultDataCell cellDecimal(DecimalFormat format){
		return new DecimalChartDataCell(format);
	}

	protected int valueIndex = 1;

	protected final DecimalFormat format;

	protected ResultDataCell(DecimalFormat format){
		this.format = format;
	}
	
	public boolean needFormat(){
		return null != format;
	}
	
	public Number readValue(Object[] its) {
		return (Number)its[valueIndex];
	}

	public void setValueIndex(int valueIndex) {
		this.valueIndex = valueIndex;
	}
	
	public abstract Object format(Number num);
	public abstract Number readValue(Number sourceValue, Number resultValue);
	
	private static class NumberChartDataCell extends ResultDataCell{
		
		protected NumberChartDataCell(DecimalFormat format){
			super(format);
		}
		
		protected NumberChartDataCell(){
			super(null);
		}
		
		public Object format(Number num){
			return format.format(num.longValue());
		}

		@Override
		public Number readValue(Number sourceValue, Number resultValue) {
			if(null == resultValue){
				resultValue = sourceValue.longValue();
			}else{
				resultValue = (Long)resultValue + sourceValue.longValue();
			}
			return resultValue;
		}
	}
	
	private static class DecimalChartDataCell extends ResultDataCell{

		protected DecimalChartDataCell(DecimalFormat format){
			super(format);
		}
		
		protected DecimalChartDataCell(){
			super(null);
		}
		
		public Object format(Number num){
			return format.format(num.doubleValue());
		}

		@Override
		public Number readValue(Number sourceValue, Number resultValue) {
			if(null == resultValue){
				resultValue = sourceValue.doubleValue();
			}else{
				resultValue = (Double)resultValue + sourceValue.doubleValue();
			}
			return resultValue;
		}
	}
}
